<?php
/**
 * Created by PhpStorm.
 * User: MrChen
 * QQ：30552056
 * Date: 15-1-19
 * Time: 下午12:59
 */
namespace Think\Storage\Driver;
use Think\Storage;
//阿里云OSS文件操作扩展
class Oss extends storage{
    private $option;
    private $contents = array();
    private function storage(){
        $storage = \Alibaba::Storage($this->option);
        return $storage;
    }
    public function __construct($option = array()){
        if(empty($option)){
            if(oss_id && oss_key && oss_bucket){
                $this->option = array('id'=>oss_id,'key'=>oss_key,'bucket'=>oss_bucket);
            }else{
                $this->option = '';
            }
        }else{
            $this->option = $option;
        }
    }
    /**
     * 读取文件内容，如果传入$path，则将文件保存到$path目录，否则返回文件内容；
     * @param $filename
     * @param string $path
     * @return bool
     */
    public function read($filename,$path=''){
        $tfilename = $filename;
        $this->ltrim($filename);
        $storage = $this->storage();
        if($storage->fileExists($filename)){
            if($path){
                $res =  $storage->get($filename,$path);
            }else{
                $res =  $storage->get($filename);
            }
            $this->contents[$tfilename] = $storage->get($filename);
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 写入文件
     * @param $filename
     * @param $content
     * @param string $exp
     * @return mixed
     */
    public function put($filename,$content,$exp=''){
        $tfilename = $filename;
        $this->ltrim($filename);
        $storage = $this->storage();
        $this->contents[$tfilename]=$content;
        return $storage->saveText($filename,$content,$exp);
    }

    /**
     * 文件追加写入
     * @access public
     * @param string $filename  文件名
     * @param string $content  追加的文件内容
     * @return boolean
     */
    public function append($filename,$content,$type=''){
        $this->ltrim($filename);
        if($old_content = $this->read($filename,$type)){
            $content =  $old_content.$content;
        }
        return $this->put($filename,$content,$type);
    }

    /**
     * 加载文件
     * @access public
     * @param string $_filename  文件名
     * @param array $vars  传入变量
     * @return void
     */
    public function load($_filename,$vars=null){
        $this->ltrim($_filename);
        if(!is_null($vars))
            extract($vars, EXTR_OVERWRITE);
        eval('?>'.$this->read($_filename));
    }
    /**
     * 文件是否存在
     * @param $filename
     * @return mixed
     */
    public function has($filename){
        $this->ltrim($filename);
        $storage = $this->storage();
        return $storage->fileExists($filename);
    }

    /**
     * 删除文件
     * @param $filename
     * @return mixed
     */
    public function unlink($filename){
        $tfilename = $filename;
        $this->ltrim($filename);
        $storage = $this->storage();
        unset($this->contents[$tfilename]);
        return $storage->delete($filename);
    }
    /**
     * 读取文件信息
     * @access public
     * @param string $filename  文件名
     * @param string $name  信息名 mtime或者content
     * @return boolean
     */
    public function get($filename,$name,$type=''){
        $tfilename = $filename;
        $this->ltrim($filename);
        $storage = $this->storage();
        if(!isset($this->contents[$tfilename])){
            if(!$this->has($filename)) return false;
            $this->contents[$tfilename]=$storage->get($filename);
        }
        $content=$this->contents[$tfilename];
        $meta = $storage->getMeta($filename);
        $time = strtotime($meta['last-modified']);
        $info   =   array(
            'mtime'     =>  $time,
            'content'   =>  $content
        );
        return $info[$name];
    }
    /**
     * Oss特殊性 根目录不需要在前边加'./'
     * @param $filename
     */
    private function ltrim(&$filename){
        $filename = ltrim($filename,'.');
        $filename = ltrim($filename,'/');
    }
}